package com.ssm.controller;
import java.util.List;

import javax.servlet.http.HttpSession;

import com.ssm.pojo.Type;
import com.ssm.pojo.Yong;
import com.ssm.service.TypeService;
import com.ssm.service.GuanService;
import com.ssm.pojo.Guan;
import com.ssm.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
@Controller
public class PageController {
    @Autowired
    private BookService bookService;

    @Autowired
    private GuanService guanService;

    @Autowired
    private TypeService typeService;

    @RequestMapping("/index")
    public String toIndex() {
        return "login";
    }

    @RequestMapping("/login")
    //这里是yong1不是yong
    public String login(Yong yong, int loginType, Model model, HttpSession session) {
        // 读者
        if (loginType == 1) {
            Yong yong1 = bookService.selectYong(yong);
            if (yong1 != null) {
                session.setAttribute("yong", yong1);
                return "redirect:/yong/index";
            } else {
                model.addAttribute("msg", "账号或密码错误");
                return "login";
            }
            //管理员
        } else if (loginType == 2) {
            Guan guan = guanService.getGuan(yong);
            if (guan != null) {
                session.setAttribute("guan", guan);
                return "redirect:/guan/index";
            } else {
                model.addAttribute("msg", "账号或密码错误");
                return "login";
            }
        }
        return "login";

    }

    @ModelAttribute("types")
    public List<Type> findAllType(HttpSession session) {
        List<Type> types = typeService.findALL();
        session.setAttribute("types", types);
        return types;
    }
}